package org.softmed.rest.editor;

import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URI;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.softmed.rest.editor.commons.SwingUtil;

public class AboutButton extends JButton implements ActionListener,
		MouseListener {

	String email = "guilhermegrg@gmail.com";
	String tabula = "http://tabula.softmed.org/";
	String googleCode = "http://code.google.com/p/tabulasoftmed/";

	JLabel emailLinkLabel = new JLabel(createHTMLLink("mailto:" + email, email));
	JLabel tabulaLinkLabel = new JLabel(createHTMLLink(tabula, tabula));
	JLabel googleCodeLabel = new JLabel(createHTMLLink(googleCode, googleCode));

	public AboutButton() {
		super("About");
		setBorder(BorderFactory.createEtchedBorder());
		addActionListener(this);
		SwingUtil.setSize(this, 65, 20);
		emailLinkLabel.addMouseListener(this);
		tabulaLinkLabel.addMouseListener(this);
		googleCodeLabel.addMouseListener(this);
	}

	public String createHTMLLink(String link, String text) {
		return "<html><head></head><body><a href=\"" + link + "\">" + text
				+ "</a></body></html>";
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == this) {
			JPanel panel = new JPanel();
			panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

			panel.add(new JLabel("Developed by Guilherme Gomes."));
			panel.add(new JLabel("Contact email:"));
			panel.add(emailLinkLabel);
			panel.add(new JLabel("More info at:"));
			panel.add(tabulaLinkLabel);
			panel.add(googleCodeLabel);

			JOptionPane.showMessageDialog(this, panel,
					"REST Service Editor alpha version 0.50.00",
					// Developed by Guilherme Gomes.
					// + "Contact email : guilhermegrg@gmail.com\n"
					// + "More info at: "
					JOptionPane.PLAIN_MESSAGE);
			return;
		}

	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		try {
			if (e.getSource() == emailLinkLabel) {
				URI url = new URI("mailto:" + email);
				Desktop.getDesktop().mail(url);
			}

			if (e.getSource() == tabulaLinkLabel) {
				URI url = new URI(tabula);
				Desktop.getDesktop().browse(url);
			}

			if (e.getSource() == googleCodeLabel) {
				URI url = new URI(googleCode);
				Desktop.getDesktop().browse(url);
			}

		} catch (Exception t) {
			t.printStackTrace();
			JOptionPane.showMessageDialog(this, t.getMessage(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

}
